package com.she.iiicoursecenter.view.tab1;

import java.util.Date;

import ken.app.helper.Helper;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseReserve;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSet;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSingle;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_CourseSelectedList;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_MainActivity;
import com.she.iiicoursecenter.activity.tab3_reservation.Tab3_MainActivity;
import com.she.iiicoursecenter.bean.CourseRceserveBundleBean;
import com.she.iiicoursecenter.bean.CourseSelectedListBundleBean;
import com.she.iiicoursecenter.bean.CourseSingleBundleBean;
import com.she.iiicoursecenter.bean.ViewChageDataBean;
import com.she.iiicoursecenter.event.connect.bean.GetContentBean;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.manager.AsyncImageLoader;
import com.she.iiicoursecenter.manager.AsyncImageLoader.ImageCallback1;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class Tab1_CourseSingleView extends CustomizedView implements OnClickListener {
	//
	
	// test only!!!
	private ImageView	mImgBack , mImgRceserve;
	private RelativeLayout mInsideView;
	private ScrollView inside_a , inside_b;
	private ViewFlipper btnManual , btnInformation;

	private LayoutInflater inflater;
	
	private AsyncImageLoader asyncImageLoader;
	
	public Tab1_CourseSingleView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init(){
		//
		inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab1_coursesingle, this, true);
		//
		
		asyncImageLoader = new AsyncImageLoader();
		
		// test only!!!
		mImgBack = (ImageView)this.findViewById(R.id.img_back);
		mImgBack.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (getApp().tab3InCourseSingle == true) {
					getApp().fadeout = true;
					Message msg = Tab1_CourseSingleView.this.getApp().mChangeViewHandler.obtainMessage();
					msg.what 	= Tab1_CourseSingleView.this.getApp().MSG_WHAT_CHANGEVIEW;
					ViewChageDataBean   dataBean 	= new ViewChageDataBean(Tab1_CourseSingle.class, Tab3_MainActivity.class, null);
					msg.obj							= dataBean;
					Tab1_CourseSingleView.this.getApp().mChangeViewHandler.sendMessage(msg);
				}else if (getApp().tab2InCourseSingle == true) {
					getApp().fadeout = true;
					Message msg = Tab1_CourseSingleView.this.getApp().mChangeViewHandler.obtainMessage();
					msg.what = Tab1_CourseSingleView.this.getApp().MSG_WHAT_CHANGEVIEW;
					CourseSelectedListBundleBean bundleBean = new CourseSelectedListBundleBean(getApp().tab2Date, getApp().tab2Slot, getApp().tab2Time);
					ViewChageDataBean dataBean = new ViewChageDataBean(Tab1_CourseSingle.class, Tab2_CourseSelectedList.class, bundleBean);
					msg.obj = dataBean;
					Tab1_CourseSingleView.this.getApp().mChangeViewHandler.sendMessage(msg);
				}else{
					getApp().fadeout = true;
					getApp().getContentSingleBeanData().setResult(901);
					Message msg = Tab1_CourseSingleView.this.getApp().mChangeViewHandler.obtainMessage();
					msg.what 	= Tab1_CourseSingleView.this.getApp().MSG_WHAT_CHANGEVIEW;
					ViewChageDataBean   dataBean 	= new ViewChageDataBean(Tab1_CourseSingle.class, Tab1_CourseSet.class, null);
					msg.obj							= dataBean;
					Tab1_CourseSingleView.this.getApp().mChangeViewHandler.sendMessage(msg);
				}
				
			}});	
		
		//inside
		mInsideView = (RelativeLayout) this.findViewById(R.id.tab1_coursesingle_inside);
		inside_a = (ScrollView) inflater.inflate(R.layout.tab1_coursesingle_inside_a, null);
		mInsideView.addView(inside_a);
		
		//button
		btnManual = (ViewFlipper) this.findViewById(R.id.btn_tab1_manual);
		btnInformation = (ViewFlipper) this.findViewById(R.id.btn_tab1_information);
		btnManual.setDisplayedChild(1);
		btnInformation.setDisplayedChild(0);
		
		btnManual.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (inside_a == null) {
					inside_a = (ScrollView) inflater.inflate(R.layout.tab1_coursesingle_inside_a, null);
				}
				mInsideView.removeAllViews();
				mInsideView.addView(inside_a);
				
				btnManual.setDisplayedChild(1);
				btnInformation.setDisplayedChild(0);
				notifyAdapter();
			}
		});
		
		btnInformation.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (inside_b == null) {
					inside_b = (ScrollView) inflater.inflate(R.layout.tab1_coursesingle_inside_b, null);
				}
				mInsideView.removeAllViews();
				mInsideView.addView(inside_b);
				
				btnManual.setDisplayedChild(0);
				btnInformation.setDisplayedChild(1);
				notifyAdapter();
			}
		});
	}		
	
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
	
	public void notifyAdapter(){
		GetContentBean data = getApp().getContentSingleBeanData(); 
		long nowTime = System.currentTimeMillis() + KenEventManager.getInstance().getServerTimeOffset();
		if (inside_a != null) {
			((TextView)inside_a.findViewById(R.id.tab1_single_a_introduction)).setText(data.getIntrodution());
			((TextView)inside_a.findViewById(R.id.tab1_single_a_object)).setText(data.getObject());
			((TextView)inside_a.findViewById(R.id.tab1_single_a_objective)).setText(data.getObjective());
			((TextView)inside_a.findViewById(R.id.tab1_single_a_outline)).setText(data.getOutline());
			if (data.getRemain() <= 0 || getApp().tab3InCourseSingle == true || nowTime > (data.getDateEnd()* 1000)) {
				((ImageView)inside_a.findViewById(R.id.tab1_torceserve)).setVisibility(View.GONE);
			}
			asyncImageLoader.loadDrawable(getContext(), data.getPic(), new ImageCallback1() {
				
				public void imageLoaded(Drawable imageDrawable, String imageUrl) {
					// TODO Auto-generated method stub
					((ImageView)inside_a.findViewById(R.id.tab1_single_a_pic)).setImageDrawable(imageDrawable);
				}
			});
			if (getApp().itemListColorCount != -1) {
				int colorCount = getApp().itemListColorCount;
				int color = 0 ;
				if (colorCount % 5 == 0) {
					color = R.drawable.raw_t_green;
				} else if (colorCount % 5 == 1) {
					color = R.drawable.raw_t_yellow;
				} else if (colorCount % 5 == 2) {
					color = R.drawable.raw_t_orange;
				} else if (colorCount % 5 == 3) {
					color = R.drawable.raw_t_purple;
				} else if (colorCount % 5 == 4) {
					color = R.drawable.raw_t_blue;
				}
				inside_a.findViewById(R.id.tab1_single_a_colorview1).setBackgroundResource(color);
				inside_a.findViewById(R.id.tab1_single_a_colorview2).setBackgroundResource(color);
				inside_a.findViewById(R.id.tab1_single_a_colorview3).setBackgroundResource(color);
				inside_a.findViewById(R.id.tab1_single_a_colorview4).setBackgroundResource(color);
			}
		}
		if (inside_b != null) {
			((TextView)inside_b.findViewById(R.id.tab1_single_b_code)).setText(data.getCode());
			((TextView)inside_b.findViewById(R.id.tab1_single_b_datestart)).setText(Helper.timestampToDate(data.getDateStart(),"yyyy年MM月dd日")+" ~ \n"+Helper.timestampToDate(data.getDateEnd(),"yyyy年MM月dd日"));
			((TextView)inside_b.findViewById(R.id.tab1_single_b_expence)).setText(data.getExpense()+"元");
			((TextView)inside_b.findViewById(R.id.tab1_single_b_hour)).setText(data.getHour()+"小時");
			((TextView)inside_b.findViewById(R.id.tab1_single_b_location)).setText(data.getLocation());
			((TextView)inside_b.findViewById(R.id.tab1_single_b_remain)).setText(data.getRemain()+"位");
			((TextView)inside_b.findViewById(R.id.tab1_single_b_remark)).setText(data.getRemark());
			((TextView)inside_b.findViewById(R.id.tab1_single_b_tel)).setText(data.getTel());
			((TextView)inside_b.findViewById(R.id.tab1_single_b_timeslot)).setText(data.getTimeSlot());
			((TextView)inside_b.findViewById(R.id.tab1_single_b_datetime)).setText(/*Helper.timestampToDate(data.getDateStart(),"yyyy年MM月dd日")+" "+*/data.getTimeStart()+"-"+data.getTimeEnd());
			if (data.getRemain() <= 0 || getApp().tab3InCourseSingle == true || nowTime > (data.getDateEnd() * 1000)) {
				((ImageView)inside_b.findViewById(R.id.tab1_torceserve)).setVisibility(View.GONE);
			}
			if (getApp().itemListColorCount != -1) {
				int colorCount = getApp().itemListColorCount;
				int color = 0 ;
				if (colorCount % 5 == 0) {
					color = R.drawable.raw_t_green;
				} else if (colorCount % 5 == 1) {
					color = R.drawable.raw_t_yellow;
				} else if (colorCount % 5 == 2) {
					color = R.drawable.raw_t_orange;
				} else if (colorCount % 5 == 3) {
					color = R.drawable.raw_t_purple;
				} else if (colorCount % 5 == 4) {
					color = R.drawable.raw_t_blue;
				}
				inside_b.findViewById(R.id.tab1_single_b_colorview1).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview2).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview3).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview4).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview5).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview6).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview7).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview8).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview9).setBackgroundResource(color);
				inside_b.findViewById(R.id.tab1_single_b_colorview10).setBackgroundResource(color);
			}
		}
		toRceserveView();
		

	}
	
	private void toRceserveView(){
		
		mImgRceserve = (ImageView) mInsideView.findViewById(R.id.tab1_torceserve);
		mImgRceserve.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Message msg = Tab1_CourseSingleView.this.getApp().mChangeViewHandler.obtainMessage();
				msg.what = Tab1_CourseSingleView.this.getApp().MSG_WHAT_CHANGEVIEW;
				CourseRceserveBundleBean bundleBean = new CourseRceserveBundleBean(getApp().getContentSingleBeanData().getContentId(), 
														getApp().getContentSingleBeanData().getName(), getApp().getContentSingleBeanData().getCode());
				ViewChageDataBean dataBean = new ViewChageDataBean(Tab1_CourseSingle.class, Tab1_CourseReserve.class, bundleBean);
				msg.obj = dataBean;
				Tab1_CourseSingleView.this.getApp().mChangeViewHandler.sendMessage(msg);
			}
		});
		
	}
	
}
